﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGourd.Core.Features;
using NGourd.Core.Steps;
using WatiN.Core;

namespace NGourd.Google.Features.Steps
{
    public class GoogleSteps : StepContainerBase
    {
        private IDictionary<string, string> Pages = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
        {
            { "home", "http://www.google.com/" }
        };

        private static IE browser;

        public override void BeforeAll()
        {
            browser = new IE();
        }

        public override void AfterAll()
        {
            if (browser != null)
            {
                browser.Close();
                browser.Dispose();
            }
        }

        [Step(@"at the (\w+) page")]
        public void GotoPage(string pageName)
        {
            Assert.That(Pages.ContainsKey(pageName), "Missing Page");
            browser.GoTo(Pages[pageName]);
        }

        [Step(@"enter ""(.*)"" into the search box")]
        public void SearchFor(string query)
        {
            browser.TextField(t => t.Name == "q").Value = query;
        }

        [Step(@"Press Go"), Step(@"Click Go")]
        public void PressGo()
        {
            browser.Button(b => b.Value == "Google Search").Click();
        }

        [Step(@"see a link to (.*)")]
        public void EnsureLinkExists(string url)
        {
            Assert.That(
                browser.Links.Any(l => l.Url.StartsWith(url)), 
                "No anchor tags that link to " + url);
        }

        [Step(@"see ""(.*)""")]
        public void ShouldSee(string text)
        {
            Assert.That(
                browser.Text.Contains(text), 
                "Looking for " + text + " but it wasn't here");
        }

        [Step(@"foo should go bar")]
        public void FooBar()
        {
            Assert.That(true, "Wee");
        }
    }
}